<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="员工号" prop="employeeId">
    <el-select v-model="queryParams.employeeId" placeholder="请选择员工号" clearable filterable>
      <el-option
          v-for="dict in employeeIdOptions"
          :key="dict.partyId"
          :label="dict.firstName"
          :value="dict.partyId"
      />
    </el-select>
  </el-form-item> -->
      <el-form-item label="姓名" prop="firstName">
        <el-input v-model="queryParams.firstName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择部门" clearable >
          <el-option v-for="dict in deptIdOptions" :key="dict.deptId" :label="dict.deptName" :value="dict.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobilePhone">
        <el-input v-model="queryParams.mobilePhone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证" prop="cardId">
        <el-input v-model="queryParams.cardId" placeholder="请输入身份证" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="员工状态" prop="emplStatusId">
        <el-select v-model="queryParams.emplStatusId" placeholder="请选择员工状态" clearable>
          <el-option v-for="dict in emplStatusIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:empl_rosters:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="empl_rostersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="员工ID" align="center" prop="partyId" /> -->
      <el-table-column label="员工号" align="center" prop="employeeId" />
      <el-table-column label="姓名" align="center" prop="firstName" />
      <el-table-column label="性别" align="center" prop="gender" :formatter="genderFormat" />
      <el-table-column label="部门" align="center" prop="deptId" :formatter="deptIdFormat" />
      <el-table-column label="职位" align="center" prop="positionId" :formatter="positionIdFormat" />
      <el-table-column label="手机号" align="center" prop="mobilePhone" width="120" />
      <el-table-column label="身份证" align="center" prop="cardId" width="180" />
      <!-- <el-table-column label="公司邮箱" align="center" prop="mail" /> -->
      <el-table-column label="员工状态" align="center" prop="emplStatusId" :formatter="emplStatusIdFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:empl_rosters:detail']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改花名册对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <span style="padding: 0px 40px 0px 30px;font-size: 24px;">{{ form.firstName }}</span>
          </el-col>
          <!-- <el-col :span="12" >
            <el-form-item label="头像">
                <image-upload v-model="form.imageData"/>
            </el-form-item>
          </el-col> -->
        </el-row>
        <!-- <el-divider content-position="center"></el-divider> -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="职位" prop="positionId">
              <span>{{ positionIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门" prop="deptId">
              <span>{{ deptIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机" prop="mobilePhone">
              <span>{{ form.mobilePhone }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="个人邮箱" prop="personalEmail">
              <span>{{ form.personalEmail }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上级主管" prop="supervisor">
              <span>{{ supervisorFormat(form) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工状态" prop="entryStatusId">
              <span>{{ emplStatusIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">员工基础信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工号" prop="employeeId">
              <span>{{ form.employeeId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <span>{{ genderFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="民族" prop="nation">
              <span>{{ nationFormat(form) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDate">
              <span>{{ form.birthDate }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="证件类型" prop="cardType">
              <span>{{ cardTypeFormat(form) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="cardId">
              <span>{{ form.cardId }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="住址" prop="homeAddress">
              <span>{{ form.homeAddress }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最高学历" prop="education">
              <span>{{ educationFormat(form) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚姻" prop="maritalStatus">
              <span>{{ maritalStatusFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12" >
            <el-form-item label="头像">
                <image-upload v-model="form.imageData"/>
            </el-form-item>
          </el-col>
        </el-row > -->
        <!-- <el-row>

        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmpl_rosters, getEmpl_rosters, delEmpl_rosters, addEmpl_rosters, updateEmpl_rosters } from "@/api/ehr/empl_rosters";

export default {
  name: "Empl_rosters",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 花名册表格数据
      empl_rostersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 员工号业务字典
      employeeIdOptions: [],
      // 性别业务字典
      genderOptions: [],
      // 部门业务字典
      deptIdOptions: [],
      // 职位业务字典
      positionIdOptions: [],
      // 民族业务字典
      nationOptions: [],
      // 上级主管业务字典
      supervisorOptions: [],
      // 学历（最高）业务字典
      educationOptions: [],
      // 学历（最高）业务字典
      educationOptions: [],
      // 政治面貌业务字典
      politicStatusOptions: [],
      // 婚姻状况业务字典
      maritalStatusOptions: [],
      // 员工状态业务字典
      emplStatusIdOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        employeeId: null,
        firstName: null,
        deptId: null,
        mobilePhone: null,
        cardId: null,
        emplStatusId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 员工号业务字典
    this.getEntityDicts("person:partyId:firstName").then((response) => {
      this.employeeIdOptions = response.data;
    });
    // 性别业务字典
    this.getEnumDicts("PERSON_GENDER").then((response) => {
      this.genderOptions = response.data;
    });
    // 部门业务字典
    this.getEntityDicts("sysDept:deptId:deptName").then((response) => {
      this.deptIdOptions = response.data;
    });
    // 职位业务字典
    this.getEntityDicts("sysPost:postId:postName").then((response) => {
      this.positionIdOptions = response.data;
    });
    // 证件类型业务字典
    this.getEnumDicts("PERSON_CARD_TYPE").then((response) => {
      this.cardTypeOptions = response.data;
    });
    // 民族业务字典
    this.getEnumDicts("NATION").then((response) => {
      this.nationOptions = response.data;
    });
    // 学历（最高）业务字典
    this.getEnumDicts("PERSON_EDUCATION").then((response) => {
      this.educationOptions = response.data;
    });
    // 政治面貌业务字典
    this.getEnumDicts("PERSON_POLITIC").then((response) => {
      this.politicStatusOptions = response.data;
    });
    // 婚姻状况业务字典
    this.getEnumDicts("PERSON_MARITAL").then((response) => {
      this.maritalStatusOptions = response.data;
    });
    // 员工状态业务字典
    this.getEnumDicts("EMPL_STATUS").then((response) => {
      this.emplStatusIdOptions = response.data;
    });

    this.getList();
  },
  methods: {
    partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    emplStatusIdFormat(row, column) {
      let val = null;
      this.emplStatusIdOptions.forEach((item, index) => {
        if (row.emplStatusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    genderFormat(row, column) {
      let val = null;
      this.genderOptions.forEach((item, index) => {
        if (row.gender === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    nationFormat(row, column) {
      let val = null;
      this.nationOptions.forEach((item, index) => {
        if (row.nation === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    cardTypeFormat(row, column) {
      let val = null;
      this.cardTypeOptions.forEach((item, index) => {
        if (row.cardType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    entryTypeFormat(row, column) {
      let val = null;
      this.entryTypeOptions.forEach((item, index) => {
        if (row.entryType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    emplTypeFormat(row, column) {
      let val = null;
      this.emplTypeOptions.forEach((item, index) => {
        if (row.emplType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    deptIdFormat(row, column) {
      let val = null;
      this.deptIdOptions.forEach((item, index) => {
        if (row.deptId === item.deptId + "") {
          val = item.deptName;
        }
      });
      return val;
    },
    positionIdFormat(row, column) {
      let val = null;
      this.positionIdOptions.forEach((item, index) => {
        if (row.positionId === item.postId + "") {
          val = item.postName;
        }
      });
      return val;
    },
    isProbationFormat(row, column) {
      let val = null;
      this.isProbationOptions.forEach((item, index) => {
        if (row.isProbation === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    supervisorFormat(row, column) {
      let val = null;
      this.employeeIdOptions.forEach((item, index) => {
        if (row.supervisor === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    educationFormat(row, column) {
      let val = null;
      this.educationOptions.forEach((item, index) => {
        if (row.education === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    politicStatusFormat(row, column) {
      let val = null;
      this.politicStatusOptions.forEach((item, index) => {
        if (row.politicStatus === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    maritalStatusFormat(row, column) {
      let val = null;
      this.maritalStatusOptions.forEach((item, index) => {
        if (row.maritalStatus === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    contractTypeFormat(row, column) {
      let val = null;
      this.contractTypeOptions.forEach((item, index) => {
        if (row.contractType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    entryStatusIdFormat(row, column) {
      let val = null;
      this.entryStatusIdOptions.forEach((item, index) => {
        if (row.entryStatusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    flowStatusFormat(row, column) {
      let val = null;
      this.flowStatusOptions.forEach((item, index) => {
        if (row.flowStatus === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    approverFormat(row, column) {
      let val = null;
      this.approverOptions.forEach((item, index) => {
        if (row.approver === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    /** 查询花名册列表 */
    getList() {
      this.loading = true;
      listEmpl_rosters(this.queryParams).then(response => {
        this.empl_rostersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partyId: null,
        employeeId: null,
        firstName: null,
        gender: null,
        deptId: null,
        positionId: null,
        mobilePhone: null,
        cardId: null,
        mail: null,
        entryDate: null,
        supervisor: null,
        imageData: null,
        nation: null,
        birthDate: null,
        residentialAddress: null,
        education: null,
        politicStatus: null,
        personalEmail: null,
        emplStatusId: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加花名册";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const partyId = row.partyId || this.ids
      getEmpl_rosters(partyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "花名册详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true,
        this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const partyId = row.partyId || this.ids
      getEmpl_rosters(partyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改花名册";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partyId != null && this.updateFlag) {
            updateEmpl_rosters(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmpl_rosters(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partyIds = row.partyId || this.ids;
      this.$modal.confirm('是否确认删除花名册编号为"' + partyIds + '"的数据项？').then(function () {
        return delEmpl_rosters(partyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/empl_rosters/export', {
        ...this.queryParams
      }, `empl_rosters_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
